package com.example.navigation.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.DividerDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import com.example.navigation.R
import com.example.navigation.data.OrderUiState
import com.example.navigation.ui.components.FormatterPriceLabel
import com.example.navigation.ui.theme.NavigationTheme

/**
 * 这个可组合组件需要一个名为 "orderUiState" 的参数，
 * 它代表订单状态；还有一个名为 "onCancelButtonClicked" 的 lambda 表达式，
 * 该表达式用于触发取消订单操作，并将最终订单信息传递给 "onSendButtonClicked" 表达式；还有一个 lambda 表达式。
 */
@Composable
fun OrderSummaryScreen(
    orderUiState: OrderUiState,
    onCancelButtonClicked: () -> Unit,
    onSendButtonClicked: (String, String) -> Unit,
    modifier: Modifier = Modifier
) {
    val resource = LocalContext.current.resources
    val numberOfCupcakes = resource.getQuantityString(
        R.plurals.cupcakes,
        orderUiState.quantity,
        orderUiState.quantity
    )
    // 使用参数加载并格式化字符串资源。
    val orderSummary = stringResource(
       R.string.order_details,
       numberOfCupcakes,
        orderUiState.flavor,
        orderUiState.date,
        orderUiState.quantity
    )
    val newOrder = stringResource(R.string.new_cupcake_order)
    // 创建一个订单汇总列表以进行展示
    val items = listOf(
        // 总结语句 1：显示选定数量
        Pair(stringResource(R.string.quantity), numberOfCupcakes),
        // 总结语句 2：显示所选口味
        Pair(stringResource(R.string.flavor), orderUiState.flavor),
        // 摘要第 3 条：显示选定的取货日期
        Pair(stringResource(R.string.pickup_date), orderUiState.date)
    )

    Column(
        modifier = Modifier,
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(
            modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium)),
            verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
        ) {
            items.forEach { item ->
                Text(item.first.uppercase())
                Text(item.second, fontWeight = FontWeight.Bold)
                HorizontalDivider(
                    thickness = dimensionResource(R.dimen.thickness_divider),
                    color = DividerDefaults.color
                )
            }
            Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_small)))
            FormatterPriceLabel(
                subtotal = orderUiState.price,
                modifier = Modifier.align(Alignment.End)
            )
        }
        Row(
            modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium))
        ) {
            Column(
                verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
            ) {
                Button(
                    modifier = Modifier.fillMaxWidth(),
                    onClick = { onSendButtonClicked(newOrder, orderSummary)}
                ) {
                    Text(stringResource(R.string.send))
                }
                OutlinedButton(
                    modifier = Modifier.fillMaxWidth(),
                    onClick = onCancelButtonClicked
                ) {
                    Text(stringResource(R.string.cancel))
                }
            }
        }
    }
}

@Preview
@Composable
fun OrderSummaryPreview() {
    NavigationTheme {
        OrderSummaryScreen(
            orderUiState = OrderUiState(0, "Test", "Test", "$300.00"),
            onCancelButtonClicked = {},
            onSendButtonClicked = { subject: String, summary: String -> },
            modifier = Modifier.fillMaxHeight()
        )
    }
}
